<template>
  <div class="clusteringPage">
    <div class="formSearch">
      <div class="searchItem">
        <span>人群名称：</span>
        <el-input
          v-model="query.crowdName"
          maxlength="20"
          placeholder="请输入内容"
        />
      </div>
      <div class="searchItem">
        <span>客户数量:</span>
        <el-input v-model="query.min" maxlength="9" />
        &nbsp;至&nbsp;
        <el-input v-model="query.max" maxlength="9" />
      </div>
      <div class="btns">
        <el-button type="primary" @click="search">查询</el-button>
        <el-button type="primary" @click="clear">重置</el-button>
        <el-button type="primary" @click="addClustering">新建人群</el-button>
        <el-button type="danger" @click="delClusteringFn((id = null))">删除</el-button>
      </div>
    </div>
    <!-- 表格 -->
    <el-table
      v-loading="tableLoading"
      :data="tableData"
      border
      :header-cell-style="{ background: '#EEF3FF', color: '#333333' }"
      tooltip-effect="dark"
      style="width: 100%"
      class="dataTable"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column
        prop="crowdName"
        label="人群名称"
        show-overflow-tooltip
      />
      <el-table-column
        prop="content"
        label="人群定义"
        show-overflow-tooltip
      />
      <el-table-column prop="users" label="人群数量" show-overflow-tooltip />
      <el-table-column label="操作" show-overflow-tooltip>
        <template #default="scope">
          <div class="btnList">
            <el-button
              type="primary"
              link
              @click="edit(scope.row.shopCrowdId)"
            >编辑</el-button>
            <el-button
              type="primary"
              link
              @click="seeCustomer(scope.row.shopCrowdId)"
            >查看客户</el-button>
            <el-popconfirm
              title="确认要删除此人群？"
              @confirm="delClusteringFn(scope.row.shopCrowdId)"
            >
              <template #reference>
                <el-button
                  class="delCls"
                  link
                  type="danger"
                >删除</el-button>
              </template>
            </el-popconfirm>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="page.page"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="page.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      background
      :total="page.total"
      style="margin: 20px 0 0 0;"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

    <el-dialog
      v-model="visible.data"
      :close-on-click-modal="false"
      title="人群客户"
      width="80%"
      top="50px"
      class="group-dialog"
    >
      <el-table
        :data="customerData"
        border
        :header-cell-style="{ background: '#EEF3FF', color: '#333333' }"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="name" label="客户昵称" show-overflow-tooltip />
        <el-table-column prop="phone" label="手机号" show-overflow-tooltip />
        <el-table-column
          prop="frequency"
          label="消费次数"
          show-overflow-tooltip
        />
        <el-table-column
          prop="total"
          label="累计消费金额（元）"
          show-overflow-tooltip
        />
        <el-table-column
          prop="lastTime"
          label="上次消费时间"
          show-overflow-tooltip
        />
        <el-table-column
          prop="time"
          label="成为客户时间"
          show-overflow-tooltip
        />
        <el-table-column
          prop="labelName"
          label="客户标签"
          show-overflow-tooltip
        />
        <el-table-column label="操作" show-overflow-tooltip>
          <template #default="scope">
            <div class="btnList">
              <el-button
                type="primary"
                link
                @click="showDetail(scope.row.buyerUserId)"
              >详情</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
    <FormVue ref="formRef" @reset="reset" />
    <CustomerDetail ref="customerDetailRef" />
  </div>
</template>

<script setup>
import { getUsers, getCrowd, deleteCrowd } from '@/api/customer'
import FormVue from './form.vue'
import CustomerDetail from '@/views/customer/customerList/customerDetail.vue'
import { defineComponent, onMounted, reactive, ref } from 'vue'

defineComponent({
  name: 'CustomerList'
})

const tableLoading = ref(false)
let visible = reactive({
  data: false,
})

let page = reactive({
  page: 1,
  pageSize: 10,
  total: 0
})
let query = reactive({
  crowdName: '',
  max: '',
  min: '',
  shopCrowdId: null,
})

let tableData = ref([]);
// 初始化查询所有数据
function getCrowdAll () {
  tableLoading.value = true
  getCrowd(Object.assign(
    {},
    page,
    query,
  )).then(res => {
    tableData.value = res.data.list
    page.total = res.data.total
  }).finally(() => {
    tableLoading.value = false
  })
}

//  查询
function search () {
  page.page = 1
  getCrowdAll()
}
// 清除
function clear () {
  query.crowdName = ''
  query.min = null
  query.max = null
  page.page = 1
  getCrowdAll()
}
function handleCurrentChange (val) {
  page.page = val
  getCrowdAll()
}
function handleSizeChange (val) {
  page.pageSize = val
  getCrowdAll()
}

const formRef = ref();
// 编辑
function edit (id) {
  formRef.value.show(id);
}
function addClustering () {
  formRef.value.show();
}

let multipleSelection = ref([]);
function handleSelectionChange (val) {
  multipleSelection.value = val
}
function delClusteringFn (id) {
  var ids = []
  if (id) {
    ids.push(id)
  } else {
    multipleSelection.value.forEach((item) => {
      ids.push(item.shopCrowdId)
    })
    if (ids.length === 0) {
      ElMessage.warning('请选择要删除的人群')
      return false
    }
  }
  deleteCrowd({ ids }).then((res) => {
    if (res.code === '') {
      ElMessage.success('删除成功')
      page.page = 1
      getCrowdAll()
    } else {
      ElMessage.error(res.message)
    }
  })
}

let customerData = ref([]);
function seeCustomer (id) {
  getUsers({ shopCrowdId: id }).then((res) => {
    if (res.code === '') {
      if (res.data.length) {
        customerData.value = res.data
      }
    } else {
      ElMessage.error(res.message)
    }
  })
  visible.data = true
}

const customerDetailRef = ref();
function showDetail (id) {
  // this.$router.push({ name: 'customerDetail', query: { buyerUserId: id }})
  customerDetailRef.value.show(id)
}
function reset () {
  getCrowdAll()
}

onMounted(() => {
  getCrowdAll()
})
</script>

<style lang="scss" scoped>
.clusteringPage {
  padding: 20px;
  margin-top: 20px;
  background-color: #FFFFFF;
  .formSearch {
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    .searchItem {
      margin: 0 20px 20px 0;
      display: flex;
      align-items: center;
      span {
        min-width: 80px;
        text-align: center;
      }
      .label {
        max-width: 200px;
        min-width: 120px;
      }
      .nums {
        max-width: 120px;
        min-width: 80px;
      }
    }
  }
}
</style>

<style scoped>
:deep(.btnList) .delCls {
  margin-left: 10px;
}
:deep(.group-dialog) .el-dialog__headerbtn .el-dialog__close {
  color: #ffffff;
}
:deep(.tagCustomer) .el-form-item__content {
  display: flex;
}
:deep(.tagCustomer) {
  display: flex !important;
}
:deep(.tagCustomer) .el-form-item__content span {
  margin: 0 5px;
}
:deep(.clusteringPage) .formSearch .el-form {
  display: flex;
}
:deep(.clusteringPage) .tagNameStyle .el-form-item__content {
  width: 300px;
}
:deep(.addTagBox) .el-form-item {
  display: flex;
}
:deep(.addTagBox) .el-form-item__content {
  width: 80%;
}
</style>
